//
//  ZYXAFNUploadFileViewController.m
//  Internet
//
//  AFNetworking 文件上传
//

#import "ZYXAFNUploadFileViewController.h"

#import "AFNetworking.h"

@interface ZYXAFNUploadFileViewController ()

@end

@implementation ZYXAFNUploadFileViewController

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 1.获得请求管理者
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    
    // 2.发送请求(做文件上传)
    // parameters : 只能放非文件参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"username"] = @"solozyx";
    
    [mgr POST:@"http://localhost:8080/MJServer/upload" parameters:params
        constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            // 一定要在这个block中添加文件参数
    
            // 加载文件数据
            NSString *file = [[NSBundle mainBundle] pathForResource:@"test.txt" ofType:nil];
            NSData *data = [NSData dataWithContentsOfFile:file];
    
            // 拼接文件参数
            [formData appendPartWithFileData:data name:@"file" fileName:@"afn_test.txt" mimeType:@"text/plain"];
     }
     
    success:^(AFHTTPRequestOperation *operation, id responseObject) {
        ZYXLog(@"上传成功----%@", responseObject);
    }
     
    failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        ZYXLog(@"上传失败----%@", error);
    }];
    
    // 只能做非文件上传的POST请求
    //    [mgr POST:<#(NSString *)#> parameters:<#(id)#> success:<#^(AFHTTPRequestOperation *operation, id responseObject)success#> failure:<#^(AFHTTPRequestOperation *operation, NSError *error)failure#>];
}

//2015-04-05 10:29:13.182 Internet[50953:1067092] 上传成功----{
//    success = "\U4e0a\U4f20\U6210\U529f";
//}

@end
